﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.Admin
{
    public partial class Note : System.Web.UI.Page
    {
        UserBl userBl = new UserBl();
        NoteBl noteBl = new NoteBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            gvList.DataSource = noteBl.GetAll();
            gvList.DataBind();
        }
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //点击编辑
            if (e.CommandName == "EditUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("NoteDetail.aspx?id=" + id);

            }
            //点击删除
            else if (e.CommandName == "DeleteUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                try
                {
                    noteBl.Delete(id);
                    LoadData();
                    MessageHelper.RegShowJSAjax(gvList, "删除留言成功", this.Page);
                }
                catch (ReferDataNotExistException)
                {
                    AlertHelper.ShowError("发生错误，请联系系统管理员！", this.Page);
                }
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewHelper.AddRowNumber(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal ltrAuthor = e.Row.FindControl("ltrAuthor") as Literal;
                Common.Note note = e.Row.DataItem as Common.Note;
                Common.User user = userBl.GetById(note.CreatorId);
                if (user != null)
                    ltrAuthor.Text = user.NickName;
                else
                    ltrAuthor.Text = AppConstant.DeleteUserNickname;
            }
        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            gvList.PageIndex = pager.CurrentPageIndex - 1;
            LoadData(); throw new NotImplementedException();
        }
    }
}